import { computed, ref } from 'vue'
import type { RandomTestingStore } from '.'
import type { InterviewQuestions } from '@/models/InterviewQuestions/InterviewQuestions'

export class RenderQuestionStore {
  constructor(rootStore: RandomTestingStore) {
    this.rootStore = rootStore
  }

  rootStore

  /**
   * 当前题目对象
   */
  question = ref<InterviewQuestions>()

  /**
   * 当前题目Id
   */
  id = computed(() => {
    const { question } = this
    return question.value?.id
  })

  /**
   * 当前题目
   */
  title = computed(() => {
    const { question } = this
    return question.value?.title || '--'
  })

  answer = computed(() => {
    const { question } = this
    return question.value?.answer || '--'
  })

  currentIndex = ref<number>(0)

  total = ref<number>(0)

  /**
   * 当前测试进度-数值
   * 0-100
   */
  progress = computed(() => {
    const { total, currentIndex } = this
    if (total.value === 0) return 100
    return ((currentIndex.value + 1) / total.value) * 100
  })

  /**
   * 当前测试进度比例-字符串
   * n/total
   */
  progressStr = computed(() => {
    const { total, currentIndex } = this
    if (total.value === 0) return '0 / 0'
    return `${currentIndex.value + 1} / ${total.value}`
  })

  hasDone = computed(() => {
    const { rootStore, id } = this
    const { randomTestStore } = rootStore
    const { doneIdList } = randomTestStore
    if (!id.value) return false
    return doneIdList.value.includes(id.value)
  })

  syncRenderData = () => {
    const { rootStore } = this
    const { randomTestStore } = rootStore
    const { question, questionCurrentIndex, showQuestionTotal } = randomTestStore
    this.question.value = question.value
    this.currentIndex.value = questionCurrentIndex.value
    this.total.value = showQuestionTotal.value
  }
}
